﻿using DocumentFormat.OpenXml.Wordprocessing;
using SPI_TOOLS.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;

namespace SPI_TOOLS.Models
{
    public class MachineStatisModel: ModelNotifyBase
    {
        public static string All_panels = "All Panels";
        #region 属性
        public string? MachineName { get; set; }

        public int TotalPanels { get; set; } = 0;

        private ObservableCollection<StatisItem> statisPanelResults = new ObservableCollection<StatisItem>();
        public ObservableCollection<StatisItem> StatisPanelResults
        {
            get { return statisPanelResults; }
            set { statisPanelResults = value; RaisePropertyChanged(nameof(StatisPanelResults)); }
        }
        private ObservableCollection<StatisItem> statisBoardResults = new ObservableCollection<StatisItem>();
        public ObservableCollection<StatisItem> StatisBoardResults
        {
            get { return statisBoardResults; }
            set { statisBoardResults = value; RaisePropertyChanged(nameof(StatisBoardResults)); }
        }

        //private ObservableCollection<StatisItem> statisMachineResults = new ObservableCollection<StatisItem>();
        //public ObservableCollection<StatisItem> StatisMachineResults
        //{
        //    get { return statisMachineResults; }
        //    set { statisMachineResults = value; RaisePropertyChanged(nameof(StatisMachineResults)); }
        //}
        //component
        private ObservableCollection<StatisItem> statisComponentResults = new ObservableCollection<StatisItem>();
        public ObservableCollection<StatisItem> StatisComponentResults
        {
            get { return statisComponentResults; }
            set { statisComponentResults = value; RaisePropertyChanged(nameof(StatisComponentResults)); }
        }

        private ObservableCollection<StatisItem> statisProductInfo = new ObservableCollection<StatisItem>();
        public ObservableCollection<StatisItem> StatisProductInfo
        {
            get { return statisProductInfo; }
            set { statisProductInfo = value; RaisePropertyChanged(nameof(StatisProductInfo)); }
        }

        #endregion
        #region 方法
        public void Clear()
        {
            StatisPanelResults.Clear();
            StatisBoardResults.Clear();
            //StatisMachineResults.Clear();
            StatisComponentResults.Clear();
            StatisProductInfo.Clear();
        }
        #region 统计逻辑
        /// <summary>
        /// 0   待检测
        /// 1   待确认
        /// 2   OK
        /// 3   NG
        /// 4   pass
        /// 5   删除
        /// 6   NG数量过多
        /// 
        private int not_check_count = 0;
        private int not_confirm_count = 0;
        private int r_ok_count = 0;
        private int r_ng_count = 0;
        private int pass_count = 0;
        private int del_count = 0;
        private int too_more_ng_count = 0;
        private int alarm_count = 0;
        //
        private int Ng_Component_Total = 0;//不良 点位总数
        private int Rok_Componen_Total = 0;//误判 点位总数
        private int Alarm_Componend_Total = 0;//报警 点位总数
        private int Pass_Componen_Total = 0;//pass,点位总数
        private int Sum_Componen_Total = 0;    //总点数

        private int Panel_Total = 0;//板子总数
        public void AddPanel(PanelDataModel panel)
        {
            if (StatisProductInfo.Count <= 0)
            {
                StatisProductInfo.Add(new StatisItem() { Name = "长宽(像素)：", Info = $"{panel.Height} X {panel.Width}" });
            }
            ++Panel_Total;
            Ng_Component_Total += panel.R_ng_Total;
            Rok_Componen_Total += panel.R_ok_Total;
            Alarm_Componend_Total += panel.AlarmTotal;
            Pass_Componen_Total += panel.ComponentTotal - panel.AlarmTotal;
            Sum_Componen_Total += panel.ComponentTotal;
            switch (panel.State)
            {
                case 0:
                    ++not_check_count;
                    break;
                case 1:
                    ++not_confirm_count;
                    break;
                case 2:
                    ++r_ok_count;
                    ++alarm_count;
                    break;
                case 3:
                    r_ng_count++;
                    ++alarm_count;
                    break;
                case 4:
                    pass_count++;
                    break;
                case 5:
                    ++del_count;
                    break;
                case 6:
                    ++too_more_ng_count;
                    break;
                default:
                    break;
            }
        }

        int board_not_check_count = 0;
        int board_not_confirm_count = 0;
        int board_r_ok_count = 0;
        int board_r_ng_count = 0;
        int board_pass_count = 0;
        int board_del_count = 0;
        int board_too_more_ng_count = 0;
        int board_alarm_count = 0;
        int board_all_count = 0;
        public void AddBoard(int state)
        {
            ++board_all_count;
            switch (state)
            {
                case 0:
                    ++board_not_check_count;
                    break;
                case 1:
                    ++board_not_confirm_count;
                    break;
                case 2:
                    ++board_r_ok_count;
                    ++board_alarm_count;
                    break;
                case 3:
                    board_r_ng_count++;
                    ++board_alarm_count;
                    break;
                case 4:
                    board_pass_count++;
                    break;
                case 5:
                    ++board_del_count;
                    break;
                //case 6:
                //    ++board_too_more_ng_count;
                //    break;
                default:
                    ++board_too_more_ng_count;
                    break;
            }
            
        }
        public void EndAdd()
        {
            StatisPanelResults.Add(new StatisItem() { Name = "总计：", Count = Panel_Total });
            if (Panel_Total == 0) return;
            StatisPanelResults.Add(new StatisItem() { Name = "未检测：", Count = not_check_count, Percent = (float)not_check_count / Panel_Total});
            StatisPanelResults.Add(new StatisItem() { Name = "未确认：", Count = not_confirm_count, Percent = (float)not_confirm_count / Panel_Total});
            StatisPanelResults.Add(new StatisItem() { Name = "复判OK：", Count = r_ok_count, Percent = (float)r_ok_count / Panel_Total});
            StatisPanelResults.Add(new StatisItem() { Name = "复判NG：", Count = r_ng_count, Percent = (float)r_ng_count / Panel_Total});
            StatisPanelResults.Add(new StatisItem() { Name = "PASS：", Count = pass_count, Percent = (float)pass_count / Panel_Total});
            StatisPanelResults.Add(new StatisItem() { Name = "删除：", Count = del_count, Percent = (float)del_count / Panel_Total});

            //component
            statisComponentResults.Add(new StatisItem() { Name = "总计：", Count = Sum_Componen_Total });
            if (Sum_Componen_Total == 0) return;
            statisComponentResults.Add(new StatisItem() { Name = "不良：", Count = Ng_Component_Total, Percent = (float)Ng_Component_Total / Sum_Componen_Total });
            statisComponentResults.Add(new StatisItem() { Name = "误判：", Count = Rok_Componen_Total, Percent = (float)Rok_Componen_Total / Sum_Componen_Total });
            statisComponentResults.Add(new StatisItem() { Name = "报警：", Count = Alarm_Componend_Total, Percent = (float)Alarm_Componend_Total / Sum_Componen_Total });
            statisComponentResults.Add(new StatisItem() { Name = "PASS：", Count = Pass_Componen_Total, Percent = (float)Pass_Componen_Total / Sum_Componen_Total });


            //board
            StatisBoardResults.Add(new StatisItem() { Name = "总计：", Count = board_all_count });
            if (board_all_count == 0) return;
            StatisBoardResults.Add(new StatisItem() { Name = "未检测：", Count = board_not_check_count, Percent = (float)board_not_check_count / board_all_count });
            StatisBoardResults.Add(new StatisItem() { Name = "未确认：", Count = board_not_confirm_count, Percent = (float)board_not_confirm_count / board_all_count });
            StatisBoardResults.Add(new StatisItem() { Name = "复判OK：", Count = board_r_ok_count, Percent = (float)board_r_ok_count / board_all_count });
            StatisBoardResults.Add(new StatisItem() { Name = "复判NG：", Count = board_r_ng_count, Percent = (float)board_r_ng_count / board_all_count});
            StatisBoardResults.Add(new StatisItem() { Name = "PASS：", Count = board_pass_count, Percent = (float)board_pass_count / board_all_count});
            StatisBoardResults.Add(new StatisItem() { Name = "删除：", Count = board_del_count, Percent = (float)board_del_count / board_all_count});

        }
        #endregion
        #region 报表数据
        public DataTable GenerateTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("统计内容", typeof(string));
            table.Columns.Add("数目", typeof(int));
            table.Columns.Add("百分比", typeof(float));
            table.Columns.Add("其他信息", typeof(string));

            foreach (var item in StatisPanelResults)
            {
                var row = table.NewRow();
                row["统计内容"] = $"panel-{item.Name}";
                row["数目"] = item.Count;
                row["百分比"] = item.Percent;
                table.Rows.Add(row);
            }

            foreach (var item in StatisBoardResults)
            {
                var row = table.NewRow();
                row["统计内容"] = $"board-{item.Name}";
                row["数目"] = item.Count;
                row["百分比"] = item.Percent;
                table.Rows.Add(row);
            }

            foreach (var item in StatisComponentResults)
            {
                var row = table.NewRow();
                row["统计内容"] = $"component-{item.Name}";
                row["数目"] = item.Count;
                row["百分比"] = item.Percent;
                table.Rows.Add(row);
            }

            foreach (var item in StatisProductInfo)
            {
                var row = table.NewRow();
                row["统计内容"] = $"Product-{item.Name}";
                row["其他信息"] = item.Info;
                table.Rows.Add(row);
            }

            return table;
        }
        #endregion
        #endregion
    }
    public class StatisItem
    {
        public string? Name { get; set; }

        public int Count { get; set; } = 0;

        public float Percent { get; set; } = 0;

        public string? Info { get; set; }
    }
}
